import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout 最基础的导航、菜单、视图布局组件 */
import Layout from '@/layout'

/* Router Modules */
import appAdmin from './modules/appAdmin'
import testDemo from './modules/testDemo'

/**
 * 面包屑和横排窗口标题都是用路由中的meta数据 注意部分功能转移到菜单里
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false) 不用
 * alwaysShow: true               if set true, will always show the root menu 不用
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb 重定向到设置的路由 如果set noRedirect将不会在breadcrumb中重定向
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)  此项不用
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)  面包屑、tag窗口标题的显示的名字 i18n会根据语言设置的值替换 注意左侧菜单数据是从后台取meta中title和此处要一致
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view 如果设置为true默认false，则标记将附加在tags-view中 就像首页一样效果
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true) 如果设置为false，则项目将隐藏在breadcrumb中（默认值为true）
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set 测试效果未知 不用
    validLogin: true             验证登录状态
    validAction: true            验证动作方法权限
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 * 系统常用页面路由在此处写死
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/error',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ErrorPages',
    meta: {
      title: 'errorPages',
      icon: '404'
    },
    children: [
      {
        path: '401',
        component: () => import('@/views/error-page/401'),
        name: 'Page401',
        meta: { title: 'page401', noCache: true }
      },
      {
        path: '404',
        component: () => import('@/views/error-page/404'),
        name: 'Page404',
        meta: { title: 'page404', noCache: true }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'dashboard', affix: true, validLogin: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 * 所有后台生成的路由json文件都在此处以children方式引入
 */
export const asyncRoutes = [
  /** when your routing map is too long, you can split it into small modules 当您的路由图太长时，您可以将其拆分为小模块 **/
  {
    path: '/appAdmin',
    name: 'appAdmin',
    component: Layout,
    meta: {
      'title': 'appAdmin',
      'validLogin': true
    },
    children: appAdmin
  },
  {
    path: '/testDemo',
    name: 'testDemo',
    component: Layout,
    meta: {
      'title': 'testDemo',
      'validLogin': true
    },
    children: testDemo
  }

]

// 创建路由实例
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
